package com.luocj.elephant.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.luocj.elephant.demo.R;
import com.luocj.elephant.demo.utils.Utils;

public class MyPieView extends View {

    private static final String TAG = MyPieView.class.getSimpleName();
    private Paint mPaint = new Paint();
    private int pieRingColor = Color.RED;
    private int result;
    private int width;
    private int height;
    private float center;

    public MyPieView(Context context) {
        this(context, null);
    }

    public MyPieView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyPieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(Utils.dp2px(getContext(),5));
        mPaint.setAntiAlias(true);
        result = Utils.dp2px(context, 100);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyPieView);

        ta.getColor(R.styleable.MyPieView_myPieRingColor, pieRingColor);

        ta.recycle();


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged: " + "w :" + Utils.px2dip(getContext(),w) + "w :" + w + "w :" + w + "w :" + w);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = result;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = result;
        }

        int min = Math.min(width, height);

        setMeasuredDimension(min, min);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0x44f);
        center = (float) getWidth() / 2;
        canvas.translate(center, center);
        RectF rectF = new RectF(-center, -center, center, center);
        canvas.drawArc(rectF,-90,90,true,mPaint);

        mPaint.setColor(Color.YELLOW);
        canvas.drawArc(rectF,0,90,true,mPaint);

        mPaint.setColor(Color.BLACK);
        canvas.drawPoint(getWidth()/2,getWidth()/2,mPaint);

    }
}
